package gold.digger;

import java.util.*;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC387 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public int firstUniqChar(String s) {
            if (s == null || s.length() <= 0) return -1;

            Map<Character, Integer> map = new HashMap<>();
            for (int i = 0; i < s.length(); i++) {
                char ch = s.charAt(i);
                if (map.containsKey(ch)) {
                    map.put(ch, map.get(ch) + 1);
                } else {
                    map.put(ch, 1);
                }
            }

            for (int i = 0; i < s.length(); i++) {
                char ch = s.charAt(i);
                if (map.get(ch).intValue() == 1) return i;
            }

            return -1;
        }
    }

    public void run() {
        Solution solution = new Solution();
        System.out.println(solution.firstUniqChar("abbbbacd"));
    }


    public static void main(String[] args) throws Exception {
        LC387 an = new LC387();
        an.run();

        System.out.println("\nanswer total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
